package com.mkl.business.controller;

import com.mkl.business.domain.Provider;
import com.mkl.business.service.ProviderService;
import com.mkl.business.vo.ProviderVo;
import com.mkl.system.common.Constant;
import com.mkl.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 9:57
 * 供应商controller
 */
@RestController
@RequestMapping("api/provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    /**
     * 根据条件加载全部供应商
     *
     * @param providerVo 供应商vo
     * @return 返回数据
     */
    @RequestMapping("loadAllProvider")
    public Object loadAllProvider(ProviderVo providerVo) {
        return providerService.loadAllProvider(providerVo);
    }

    /**
     * 新增供应商
     *
     * @param provider 供应商
     * @return 返回结果
     */
    @RequestMapping("addProvider")
    public ResultObj addProvider(Provider provider) {
        try {
            provider.setAvailable(Constant.AVAILABLE_TRUE);
            providerService.saveProvider(provider);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新供应商
     *
     * @param provider 供应商
     * @return 返回结果
     */
    @RequestMapping("updateProvider")
    public ResultObj updateProvider(Provider provider) {
        try {
            providerService.updateProvider(provider);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id删除供应商
     *
     * @param id 供应商id
     * @return 返回结果
     */
    @RequestMapping("deleteProvider")
    public ResultObj deleteProvider(Integer id) {
        try {
            providerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除供应商
     *
     * @param ids 供应商ids
     * @return 返回结果
     */
    @RequestMapping("batchDeleteProvider")
    public ResultObj batchDeleteProvider(Integer[] ids) {
        try {
            List<Integer> idsList = new ArrayList<>(Arrays.asList(ids));
            providerService.removeByIds(idsList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 商品下拉查询所有供应商
     *
     * @return 返回结果
     */
    @RequestMapping("getAllAvailableProvider")
    public Object getAllAvailableProvider() {
        return providerService.getAllAvailableProvider();
    }


}
